package com.pptree.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(urlPatterns={"*.action"})
public class MainServlet extends HttpServlet {
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//解决POST请求方式出现的中文乱码
		//request.setCharacterEncoding("utf-8");
		//response.setContentType("text/html;charset=utf-8");
		
		
		
		/*PrintWriter out = response.getWriter();
		String method = request.getParameter("method");
		if(method == null || "".contentEquals(method)){
			//throw new IllegalArgumentException("非法参数，请检查URL编写是否正确！"); //url?method=add
			response.sendError(500, "非法URL参数，请检查URL编写是否正确!");
			return;
		}
		
		try {
			if("add".contentEquals(method)){
				addNews(request,response);
			}
			else if("query".contentEquals(method)){
				queryNewsList(request, response);
			}
			else if("delete".contentEquals(method)){
				deleteNews(request, response);
			}
			else if("update".contentEquals(method)){
				updateNews(request, response);
			}
			else{
				response.sendError(500, "非法URL参数，请检查URL编写是否正确!");
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
		out.flush();
		out.close();*/
		
		
		//使用反射机制动态调用方法
		//获取方法名字
        String servletPath = request.getServletPath();//获得servlet路径
        System.out.println("servletPath------->" + servletPath); // /add_News.action
        // 去掉斜杠和.do
        String methodName = servletPath.substring(1, servletPath.length() - 7);//add_News
        System.out.println("methodName---->" + methodName);
        
        try {
        	
        	//com.pptree.servlet.UserController
        	//com.pptree.servlet.NewsController
        	//com.pptree.servlet.CommmentController
        	//com.pptree.servlet.CategoryController
        	
        	//http://localhost:8080/web项目名/add_News.action?newsid=234&pageno=2
        	
        	String[] props = methodName.split("_");//String[] = {"add","News"}
        	
        	String className = props[1];
        	
        	Class clazz = Class.forName("com.pptree.servlet."+className+"Controller");
        	
            // 利用反射获取方法
            @SuppressWarnings("unchecked")
			Method method = clazz.getDeclaredMethod(methodName,
                    HttpServletRequest.class, HttpServletResponse.class);
            // 执行相应的方法
            method.invoke(clazz.newInstance(), request, response);
        }
        catch(NoSuchMethodException e){
        	 e.printStackTrace();
        	 throw new RuntimeException("没有找到"+methodName+"方法，请检查该方法名是否存在！");  
        } 
        catch (Exception e) {
        	 e.printStackTrace();
        	 throw new RuntimeException("你调用的方法"+methodName+",内部发生了异常！");
        }
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		this.doGet(request, response);
	}

}
